﻿using System;
using System.Text;
using Bintje.bintje;
using System.Collections.Generic; 

namespace Bintje
{
    /// <summary>
    /// Encapsulate the user-relate functionality for Bintje SOAP API
    /// </summary>
    public class User : RepoObject
    {
        #region fields

        private API m_API;
        private imageoSession m_session;

        private string m_nickName;
        private string m_login;
        private Image m_userImage;
        private string m_password;
        private int m_mobile_client_id;
        private UserPreferences m_preferences;
        private string m_imageUrl;
        private long m_created1;
        private long m_modified1;
        private IList<Trail> m_trails;
        private bool m_professional;
        private string m_userLayers;

        #endregion
        
        #region properties

        public string NickName
        {
            get { return m_nickName; }
            set { m_nickName = value; }
        }

        public Image UserImage
        {
            get { return m_userImage; }
            set { m_userImage = value; }
        }

        public string Login
        {
            get { return m_login; }
            set { m_login = value; }
        }

        public string Password
        {
            get { return m_password; }
            set { m_password = value; }
        }
        
        public int UserID 
        {
            get { return m_session.userId; }
        }

        public UserPreferences Preferences
        {
            get { return m_preferences; }
            set { m_preferences = value; }
        }

        public int MobileClientId
        {
            get { return m_mobile_client_id; }
            set { m_mobile_client_id = value; }
        }

        public string ImageUrl
        {
            get { return m_imageUrl; }
        }

        public long Modified1
        {
            get { return m_modified1; }
            set { m_modified1 = value; }
        }

        public long Created1
        {
            get { return m_created1; }
            set { m_created1 = value; }
        }

        public imageoSession Session
        {
            get { return m_session; }
        }

        public IList<Trail> Trails 
        {
            get 
            {
                if (null == m_trails)
                {
                    m_trails = Trail.Load(m_session);
                }
                return m_trails;
            }
        }

        public bool Professional
        {
            get { return m_professional; }
            set { m_professional = value; }
        }
        
        public string UserLayers
        {
            get { return m_userLayers; }
            set { m_userLayers = value; }
        }

        #endregion

        #region ctor

        public User()
        {
            m_API = API.instance;
        }

        public User(string fullname, string login)
        {
            m_nickName = fullname;
            m_login = login;
            m_API = API.instance;
        }

        public User(string fullname, string login, string password)
        {
            m_nickName = fullname;
            m_login = login;
            m_password = password;
            m_API = API.instance;
        }

        public User(imageoSession session) 
        {
            m_session = session;
        }

        public User(user user, string url) 
        {
            m_id = user.id;
            m_login = user.login;
            m_mobile_client_id = user.mobile_client_id;
            m_nickName = user.nickName;
            m_password = user.password;
            if (null != user.preferences)
                m_preferences = new UserPreferences(user.preferences);
            if (null != user.image)
            {
                if (!string.IsNullOrEmpty(url))
                    m_userImage = new Image(user.image, url);
                else
                    m_userImage = new Image(user.image);
            }
            m_professional = user.professional;
            m_userLayers = user.userlayers;
        }

        #endregion

        #region methods

        public bool doLogin()
        {
            loginResponse logResp = m_API.doLogin(m_login, m_password);
            if (!string.IsNullOrEmpty(logResp.message))
            {
                return false;
            }
            else if (logResp.status.ToUpper() == API.RESPONSE_SUCCESS)
            {
                m_session = logResp.session;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool doLogout()
        {
            if (m_session == null)
            {
                return true;
            }
            logoutResponse outResp = m_API.doLogout(m_session);
            if (!string.IsNullOrEmpty(outResp.message))
            {
                return false;
            }
            else if (outResp.status.ToUpper() == API.RESPONSE_SUCCESS)
            {
                m_session = null;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool register()
        {
            registerRequest req = new registerRequest();
            req.fullName = m_nickName;
            req.loginName = m_login;

            registerResponse resp = m_API.registerUser(req);
            if (!string.IsNullOrEmpty(resp.message))
            {
                return false;
            }
            else if (resp.status.ToUpper() == API.RESPONSE_SUCCESS)
            {
                m_session = resp.session;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool retrievePassword()
        {
            registerRequest req = new registerRequest();
            req.fullName = m_nickName;
            req.loginName = m_login;

            registerResponse resp = m_API.resendPassword(req);
            if (!string.IsNullOrEmpty(resp.message))
            {
                return false;
            }
            else if (resp.status.ToUpper() == API.RESPONSE_SUCCESS)
            {
                m_session = resp.session;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool LoadDetails() 
        {
            userRequest request = GetRequest();
            userResponse response = API.instance.getUserDetails(request);
            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                m_id = response.user.id;
                if(null != response.user.image)
                    m_userImage = new Image(response.user.image);
                m_login = response.user.login;
                m_mobile_client_id = response.user.mobile_client_id;
                m_nickName = response.user.nickName;
                m_password = response.user.password;
                if(null != response.user.preferences)
                    m_preferences = new UserPreferences(response.user.preferences);
                m_imageUrl = response.imageUrl;
                if (null!= response.user && null != response.user.image)
                    if (!string.IsNullOrEmpty(response.imageUrl))
                        m_userImage = new Image(response.user.image, response.imageUrl);
                    else
                        m_userImage = new Image(response.user.image);
                return true;
            }
            return false;
        }

        public User Update()
        {
            editUserRequest request = GetEditRequest();
            userResponse response = API.instance.editUser(request);
            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                User user = new User(response.user, response.imageUrl);
                return user;
            }
            return null;
        }

        private userRequest GetRequest()
        {
            userRequest request = new userRequest();
            request.session = this.Session;
            request.userId = this.Session.userId;
            request.loginName = m_login;
            request.name = m_nickName;
            return request;
        }

        private editUserRequest GetEditRequest()
        {
            editUserRequest request = new editUserRequest();
            request.session = this.Session;
            request.user = this.ConvertToAPI();
            return request;
        }

        private user ConvertToAPI() 
        {
            user user = new user();
            user.created = m_created1;
            user.created1 = m_created1;
            user.modified = m_modified1;
            user.modified1 = m_modified1;
            user.id = m_id;
            user.image = m_userImage.ConvertToAPI();
            user.login = m_login;
            user.nickName = m_nickName;
            user.password = m_password;
            user.professional = m_professional;
            user.userlayers = m_userLayers;
            user.preferences = m_preferences.ConvertToAPI();
            return user;
        }

        #endregion
    }
}
